<?php

/**
 * @author gbfun <gbfun@163.com>  2018-06-20 16:28
 * http://www.wuweiyou.com
 */

class db_model_member_address
{
    protected $_db_table;   
    protected $_formatter;
    
    protected static $_instance;
    
    public static function get_instance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();    //by gbfun 又是静态变量
        }
    
        return self::$_instance;
    }
    
    private function __clone()
    {
    }
    
    protected function __construct()
    {
    }

    public function get_db_table()
    {
        if (null === $this->_db_table) {
            $this->_db_table = db_table_member_address::get_instance();
        }
        
        return $this->_db_table;
    }
    
    public function get_formatter()
    {
        if (null === $this->_formatter) {
            $this->_formatter = formatter_member_address::get_instance();
        }
    
        return $this->_formatter;
    }
    
    public function set_rows(& $rows)
    {
        foreach ($rows as &$row) {
            $this->set_row($row);            
        }
    }
    
    public function set_row(& $row)
    {
        $formatter = $this->get_formatter();
        
//         $formatter->set_by_create_time($row);
//         $formatter->set_by_update_time($row);
//         $formatter->set_by_status($row);
    }
    
    public function set_rows_user(& $rows)
    {
        foreach ($rows as &$row) {
            $this->set_row_user($row);
        }
    }
    
    public function set_row_user(& $row)
    {
//         $this->get_formatter()->set_by_create_time($row);
//         $this->get_formatter()->set_by_update_time($row);
    }
    
    public function find($id)
    {
        $row = $this->get_db_table()->find($id);
        //var_dump($row); exit();
    
        if(!empty($row)){
            $this->set_row($row);
            //var_dump($row); exit();
        }
    
        return $row;
    }
    
    public function fetch_row($where)
    {
        $row = $this->get_db_table()->fetch_row($where);
        //var_dump($row); exit();
    
        if(!empty($row)){
            $this->set_row($row);
            //var_dump($row); exit();
        }
    
        return $row;
    }
    
    public function fetch_all($where = null, $order = null)
    {
        $rows = $this->get_db_table()->fetch_all($where, $order);
        //var_dump($rows); exit();
    
        $this->set_rows($rows);
        //var_dump($rows); exit();
    
        return $rows;
    }
    
    public function get_list($where = null, $order = null, $options = array())
    {
        $list = $this->get_db_table()->get_list($where, $order, $options);
        //var_dump($list); exit();
    
//         $rows = $list['list'];
//         $this->set_rows($rows);
//         $list['list'] = $rows;
        $this->set_rows($list['list']);
        //var_dump($list); exit();
    
        return $list;
    }
    
    public function get_simple_list($where = null, $order = null, $options = array())
    {
    
    }    
    
    public function get_address_list($member_id, $province_list)
    {
        //var_dump($province_list); exit();
        
        $address_list = array();
        $city_list_look_up = array();
        $district_list_look_up = array();
        $street_list_look_up = array();
    
        $where = array('member_id' => $member_id);
        $list_temp  = $this->fetch_all($where);
        //var_dump($list_temp); exit();
            
        $model_foreground_address_region = wwy_resource_container::get_model_foreground_address_region();
        //var_dump($model_foreground_address_region); exit();
        foreach ($list_temp as $address) {                        
            if(empty($city_list_look_up[$address['province_id']])){
                $city_list_look_up[$address['province_id']] = $model_foreground_address_region->get_city_list($address['province_id']);
            }            
            $address['city_list'] = $city_list_look_up[$address['province_id']];
            
            if(empty($district_list_look_up[$address['city_id']])){
                $district_list_look_up[$address['city_id']] = $model_foreground_address_region->get_district_list($address['city_id']);
            }
            $address['district_list'] = $district_list_look_up[$address['city_id']];
            
            if(empty($street_list_look_up[$address['district_id']])){
                $street_list_look_up[$address['district_id']] = $model_foreground_address_region->get_street_list($address['district_id']);
            }
            $address['street_list'] = $street_list_look_up[$address['district_id']];
            
            $address['province_name'] = $province_list[$address['province_id']]['name'];
            $address['city_name']     = $address['city_list'][$address['city_id']]['name'];
            $address['district_name'] = $address['district_list'][$address['district_id']]['name'];
            $address['street_name']   = !empty($address['street_list'][$address['street_id']]) ? 
                $address['street_list'][$address['street_id']]['name'] : '';
            
            $address_list[$address['id']] = $address;
        }
        //var_dump($address_list); exit();
    
        return $address_list;
    }
    
    public function add($member_address_info)
    {
        $this->set_row_user($member_address_info);
        //var_dump($member_address_info); exit();
    
        $add_result = $this->get_db_table()->add($member_address_info);
        //var_dump($add_result);
    
        return $add_result;
    }
    
    public function update($member_address_info)
    {
        $update_result = $this->get_db_table()->update($member_address_info);
        //var_dump($update_result);
    
        return $update_result;
    }
    
    public function delete(array $ids)
    {
        //var_dump($where); exit();
    
        $delete_result = $this->get_db_table()->delete($ids);
        //var_dump($delete_result);
    
        return $delete_result;
    }
}

